<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/common/taglibs.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title><s:if test="keywordsRecommend.kwrecommendId==null"><s:text name="keywordsRecommend.add.title"/></s:if><s:else><s:text name="keywordsRecommend.edit.title"/></s:else></title>
	<%@ include file="/common/meta.jsp"%>
	<%@ include file="/common/common.jsp"%>
</head>
<body>
<div class="wrapper">
	<div class="curmb"><s:text name="your.site.position"/>:<a href="${ctx}/welcome.action"><s:text name="homepage"/></a><span>></span><s:text name="keywordsRecommend.title"/></div>
	<div class="listtit"><p><span><strong><s:if test="keywordsRecommend.kwrecommendId==null"><s:text name="keywordsRecommend.add.title"/></s:if><s:else><s:text name="keywordsRecommend.edit.title"/></s:else></strong></span></p></div>
	<div class="listadd">
		<s:form action="keywordsrecommend-save" id="keywordsrecommend_save">
		<s:hidden name="keywordsRecommend.keywords.keywordsId"></s:hidden>
		<table class="addgrid" id="listTab">
			<tr>
				<td align="right"><input type="button" onclick="add();" value="添加"  class="btnlist2" style="margin-right: 50px;"/></td>
			</tr>
			<s:iterator value="page.result">
			<tr name="recommends">
				<td>
					<table class="addgrid">
						<tr>
							<th width="15%"><span style="color:red;">*</span><s:text name="keywordsRecommend.recommendName"/>:</th>
							<td width="35%">
							<s:hidden name="kwrecommendId"></s:hidden>
							<s:textfield name="recommendName" maxlength="20" required="true" requiredposition="right" cssClass="txtin"/>
							<input type="button" onclick="delRec(this,'<s:property value='kwrecommendId'/>')" value="删除" class="btnlist2"/>
							<span style="color: #ff0000" name="span_recommendName"></span>
							</td>
							<th><s:text name="keywordsRecommend.recommendtype"/>:</th>
							<td>
							<s:select list="#{4:'服务',3:'企业',2:'产品',1:'荣誉',0:'其他'}" name="recommendtype" cssClass="txtin"></s:select>
							</td>
						</tr>
						<tr>
							<th><span style="color:red;">*</span><s:text name="keywordsRecommend.recommendurl"/>:</th>
							<td colspan="3">
							<s:textfield name="recommendurl" maxlength="100" required="true" requiredposition="right" cssClass="txtin txtin450"/>
							<span style="color: #ff0000" name="span_recommendurl"></span>
							</td>
						</tr>
						<tr>
							<th><span style="color:red;">*</span><s:text name="keywordsRecommend.introduction"/>:</th>
							<td colspan="3">
							<s:textarea name="introduction" required="true" requiredposition="right" cssClass="txtin450" cssStyle="height:40px;"/>
							<span style="color: #ff0000" name="span_introduction"></span>
							</td>
						</tr>
					</table>
				</td>
			</tr>	
			</s:iterator>
			<s:if test="page.result.size()==0">
			<tr name="recommends">
				<td>
					<table class="addgrid">
						<tr>
							<th  width="15%"><span style="color:red;">*</span><s:text name="keywordsRecommend.recommendName"/>:</th>
							<td width="35%">
							<s:hidden name="kwrecommendId"></s:hidden>
							<s:textfield name="recommendName" maxlength="20" required="true" requiredposition="right" cssClass="txtin"/>
							<input type="button" onclick="delRec(this,'')" value="删除" class="btnlist2"/>
							<span style="color: #ff0000" name="span_recommendName"></span>
							</td>
							<th><s:text name="keywordsRecommend.recommendtype"/>:</th>
							<td>
							<s:select list="#{4:'服务',3:'企业',2:'产品',1:'荣誉',0:'其他'}" name="recommendtype" cssClass="txtin"></s:select>
							</td>
						</tr>
						<tr>
							<th><span style="color:red;">*</span><s:text name="keywordsRecommend.recommendurl"/>:</th>
							<td colspan="3">
							<s:textfield name="recommendurl" maxlength="100" required="true" requiredposition="right" cssClass="txtin txtin450"/>
							<span style="color: #ff0000" name="span_recommendurl"></span>
							</td>
						</tr>
						<tr>
							<th><span style="color:red;">*</span><s:text name="keywordsRecommend.introduction"/>:</th>
							<td colspan="3">
							<s:textarea name="introduction" required="true" requiredposition="right" cssClass="txtin450" cssStyle="height:40px;"/>
							<span style="color: #ff0000" name="span_introduction"></span>
							</td>
						</tr>
					</table>
				</td>
			</tr>	
			</s:if>
			<tr id="btn_tr">
				<td align="center">
					<input type="button" value="保存修改" onclick="return chkform();" class="btnlist2"/>
					<input type="button" value="返回" onclick="window.location.href='keywords-list.action'" class="btnlist2"/>
				</td>
			</tr>								
		</table>
		</s:form>
	</div>
</div>
<table  id="res" style="display: none;">
	<tr name="recommends">
		<td>
			<table class="addgrid">
				<tr>
					<th  width="15%"><span style="color:red;">*</span><s:text name="keywordsRecommend.recommendName"/>:</th>
					<td width="35%">
					<s:hidden name="kwrecommendId"></s:hidden>
					<s:textfield name="recommendName" maxlength="20" required="true" requiredposition="right" cssClass="txtin"/>
					<input type="button" onclick="delRec(this,'')" value="删除" class="btnlist2"/>
					<span style="color: #ff0000" name="span_recommendName"></span>
					</td>
					<th><s:text name="keywordsRecommend.recommendtype"/>:</th>
					<td>
					<s:select list="#{4:'服务',3:'企业',2:'产品',1:'荣誉',0:'其他'}" name="recommendtype" cssClass="txtin"></s:select>
					</td>
				</tr>
				<tr>
					<th><span style="color:red;">*</span><s:text name="keywordsRecommend.recommendurl"/>:</th>
					<td colspan="3">
					<s:textfield name="recommendurl" maxlength="100" required="true" requiredposition="right" cssClass="txtin txtin450"/>
					<span style="color: #ff0000" name="span_recommendurl"></span>
					</td>
				</tr>
				<tr>
					<th><span style="color:red;">*</span><s:text name="keywordsRecommend.introduction"/>:</th>
					<td colspan="3">
					<s:textarea name="introduction" required="true" requiredposition="right" cssClass="txtin450" cssStyle="height:40px;"/>
					<span style="color: #ff0000" name="span_introduction"></span>
					</td>
				</tr>
			</table>
		</td>
	</tr>
</table>
</body>
</html>
<script type="text/javascript" defer="defer">
	function chkform(){
		var kwrecommendIds = $("#listTab input[name='kwrecommendId']");
		var recommendNames = $("#listTab input[name='recommendName']");
		var recommendurls = $("#listTab input[name='recommendurl']");
		var introductions = $("#listTab textarea[name='introduction']");
		var strRegex = /^[a-zA-z]+:\/\/(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?/;
		var re=new RegExp(strRegex); 
		if(kwrecommendIds.length==0){
			alert("请至少添加一个，或者点击返回");
			return false;
		}
		var flag = true;
		for(var i=0;i<kwrecommendIds.length;i++){
			if($.trim($(recommendNames[i]).val())==''){
				$("#listTab span[name='span_recommendName']")[i].innerHTML = "不能为空";
				flag = false;
				break;
			}
			else{
				$("#listTab span[name='span_recommendName']")[i].innerHTML = "";
				flag = true;
			}
			
			if($.trim($(recommendurls[i]).val())==''){
				$("#listTab span[name='span_recommendurl']")[i].innerHTML = "不能为空";
				flag = false;
				break;
			}
			else if(!re.test($(recommendurls[i]).val())){
				$("#listTab span[name='span_recommendurl']")[i].innerHTML = "格式不正确";
				flag = false;
				break;
			}
			else{				
				$("#listTab span[name='span_recommendurl']")[i].innerHTML = "";
				flag = true;
			}
			
			if($.trim($(introductions[i]).val())==''){
				$("#listTab span[name='span_introduction']")[i].innerHTML = "不能为空";
				flag = false;
				break;
			}
			else{
				$("#listTab span[name='span_introduction']")[i].innerHTML = "";
				flag = true;
			}
		}	
		
		if(!flag){
			return ;
		}
		for(var i=0;i<recommendurls.length-1;i++){
			for(var k=i+1;k<recommendurls.length;k++){
				if($(recommendurls[i]).val()==$(recommendurls[k]).val()){
					flag = false;
					$("#listTab span[name='span_recommendurl']")[i].innerHTML = "链接相同!";
					$("#listTab span[name='span_recommendurl']")[k].innerHTML = "链接相同!";
					return ;
				}
			}			
		}
		
		$.ajax({
			type:"POST",
			async:false,
			dataType:'json',
			url:"keywordsrecommend-checkurl.action?random="+Math.random(),
			data:$("#keywordsrecommend_save").serialize(),
			success:function(data){
				if(data.flag){
					$("#listTab span[name='span_recommendurl']")[data.index].innerHTML = "链接已存在!";
					flag = false;
				}										
			}
		});
		if(flag){
			$("#keywordsrecommend_save").submit();	
		}
	}
	function add(){
		var tr = $("#res tr").eq(0);
		var ss = tr.parent();
		$("#btn_tr").before(ss.html())
	}
	
	function delRec(obj,id){
		if(!confirm("确定删除?")){
			return ;
		}
		
		if($.trim(id)!=''){
			$.get("keywordsrecommend-delete.action",{ key: id});
		}
		
		var ss = $(obj);
		while(ss.attr("name")!='recommends'){
			ss = ss.parent();
		}
		ss.remove();
		
	}
	
	$(document).ready(function(){
		var keyid = $("input[name='keywordsRecommend.keywords.keywordsId']").val();
		if($.trim(keyid)==''){
			alert("参数错误");
			window.history.go(-1);
		}
	})
</script>